package com.jic.member.controller.power;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.power.MegradeType;
import com.jic.member.request.power.MegradeTypeRequest;
import com.jic.member.response.power.MegradeTypeResponse;
import com.jic.member.service.power.MegradeTypeService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@ApiOperation(value = "会员等级类型controller")
@RestController
@RequestMapping("/megradeRuleType")
@Slf4j
public class MegradeTypeController {


    @Autowired
    MegradeTypeService megradeType;

    /**
     * 分页查询
     * @author : zxl
     * @param form
     * @return
     */
    @ApiOperation(value = "获取会员等级类型列表")
    @ResponseBody
    @RequestMapping("/queryMegradeTypeList")
    public RestResult<PageResult<MegradeTypeResponse>> queryMegradeTypeList(@RequestBody MegradeTypeRequest form) {
        log.info("开始进入等级权益关联列表=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MegradeType memberRequest = BeanUtil.copy(form, MegradeType.class);
        PageResult<MegradeType> result = megradeType.queryPage(memberRequest, page);
        List<MegradeTypeResponse> memberResponseList = result.getRows().stream().map(temp -> {
            MegradeTypeResponse memberResponse = BeanUtil.copy(temp, MegradeTypeResponse.class);
            return memberResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<MegradeTypeResponse> pageResult = new PageResult<>();
        pageResult.setRows(memberResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }


    /**
     * @author : zxl
     * @param form
     * @return
     */
    @ApiOperation(value = "保存方法")
    @ResponseBody
    @RequestMapping("/saveGradeType")
    public RestResult saveGradeType(@RequestBody MegradeTypeRequest form){
        MegradeType memberRequest = BeanUtil.copy(form, MegradeType.class);
        return RestResult.success(megradeType.insert(memberRequest));
    }

    /**
     * @author : zxl
     * @param form
     * @return
     */
    @ApiOperation(value = "删除方法")
    @ResponseBody
    @RequestMapping("/deleteGradeType")
    public RestResult deleteGradeType(@RequestBody MegradeTypeRequest form){
        RestResult delete = megradeType.delete(form.getId().longValue());
        return delete;
    }


    /**
     * @author : zxl
     * @param form
     * @return
     */
    @ApiOperation(value = "查询会员等级类型不分页")
    @ResponseBody
    @RequestMapping("/queryGradeTypeInfo")
    public RestResult queryGradeTypeInfo(@RequestBody MegradeTypeRequest form){
        MegradeType memberRequest = BeanUtil.copy(form, MegradeType.class);
        return RestResult.success(megradeType.query(memberRequest));
    }

}
